package qlsl.androiddesign.view.subview.activityview;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import com.qlsl.androiddesign.kuaiwa.R;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;
import qlsl.androiddesign.activity.subactivity.BrandActivity;
import qlsl.androiddesign.adapter.subadapter.BrandAdapter;
import qlsl.androiddesign.entity.otherentity.CarBrand;
import qlsl.androiddesign.http.service.subservice.CarService;
import qlsl.androiddesign.listener.OnTextChangeListener;
import qlsl.androiddesign.util.commonutil.Log;
import qlsl.androiddesign.view.baseview.FunctionView;
import qlsl.androiddesign.view.commonview.firstsort.SideBar;
import qlsl.androiddesign.view.commonview.firstsort.SideBar.OnTouchingLetterChangedListener;
import qlsl.androiddesign.view.subview.activityview.peoplecenterbase.PeopleCenterBaseView;
import se.emilsjolander.stickylistheaders.StickyListHeadersListView;

/**
 * 品牌列表页<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
public class BrandView extends FunctionView<BrandActivity> implements
		OnItemClickListener, OnTouchingLetterChangedListener {

	private EditText et_search;

	private StickyListHeadersListView stickyView;

	private SideBar sideBar;

	private List<CarBrand> list_all = new ArrayList<CarBrand>();

	private List<CarBrand> list_current = new ArrayList<CarBrand>();

	public BrandView(BrandActivity activity) {
		super(activity);
		setContentView(R.layout.activity_select_brand);
	}

	protected void initView(View view) {
		setTitle("品牌");
		et_search = findViewById(R.id.et_search);
		stickyView = findViewById(R.id.stickyView);
		stickyView.setStickyHeaderTopOffset(0);
		stickyView.setAreHeadersSticky(true);
		stickyView.setDrawingListUnderStickyHeader(true);
		stickyView.setFastScrollEnabled(false);
		stickyView.setFastScrollAlwaysVisible(false);
		sideBar = findViewById(R.id.sidebar);
		TextView tv_dialog = findViewById(R.id.tv_dialog);
		sideBar.setTextView(tv_dialog);
	}

	protected void initData() {
		queryCarBrandList();
	}

	protected void initListener() {
		stickyView.setOnItemClickListener(this);
		et_search.addTextChangedListener(onTextChangeListener);
		sideBar.setOnTouchingLetterChangedListener(this);
	}

	@SuppressWarnings("unchecked")
	public <T> void showData(T... t) {
		if (t[0] instanceof HashMap) {
			HashMap<String, Object> map = (HashMap<String, Object>) t[0];
			List<CarBrand> list_result = (List<CarBrand>) map.get("list");

			// List<CarBrand> list_result = (List<CarBrand>) t[0];
			list_all.clear();
			list_all.addAll(list_result);
			list_current.clear();
			list_current.addAll(list_result);
			notifyDataSetChanged();
		}
	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {

	}

	private void queryCarBrandList() {
		CarService.queryBrand(this, activity);
	}

	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		CarBrand CarBrand = (CarBrand) stickyView.getItemAtPosition(position);
		Intent intent = new Intent();
		intent.putExtra("brand", CarBrand);
		activity.setResult(0, intent);
		activity.finish();
	}

	private void notifyDataSetChanged() {
		BrandAdapter adapter = (BrandAdapter) stickyView.getAdapter();
		if (adapter == null) {
			adapter = new BrandAdapter(activity, list_current);
			stickyView.setAdapter(adapter);
		} else {
			adapter.notifyDataSetChanged();
		}
	}

	private OnTextChangeListener onTextChangeListener = new OnTextChangeListener() {

		public void afterTextChanged(android.text.Editable s) {
			if (list_all.size() > 0) {
				if (s.length() > 0) {
					List<CarBrand> list_result = new ArrayList<CarBrand>();
					for (CarBrand CarBrand : list_all) {
						if (CarBrand.getName().contains(s.toString())) {
							list_result.add(CarBrand);
						}
					}
					list_current.clear();
					list_current.addAll(list_result);
					Collections.sort(list_current);
					notifyDataSetChanged();
				} else {
					list_current.clear();
					list_current.addAll(list_all);
					Collections.sort(list_current);
					notifyDataSetChanged();
				}
			}
		};

	};

	public void onTouchingLetterChanged(String letter) {
		for (int index = 0; index < list_current.size(); index++) {
			String currentLetter = list_current.get(index).getInitials();
			if (currentLetter.compareTo(letter) >= 0) {
				stickyView.smoothScrollToPosition(index);
				return;
			}
		}
	}
}
